package team.knowers.qzhserver.user.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import team.knowers.qzhserver.common.service.RedisService;
import team.knowers.qzhserver.user.user.entity.dao.UserDO;
import team.knowers.qzhserver.user.user.service.UserCacheService;

/**
 * 用户缓存管理Service实现类
 *
 * @author xtg
 * @date 2024/2/15 13:31
 */
@Service
public class UserCacheServiceImpl implements UserCacheService {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private RedisService redisService;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.user}")
    private String REDIS_KEY_USER;

    @Override
    public void delUser(String id) {
            String key = REDIS_KEY_USER + ":" + id;
            String key2 = REDIS_KEY_USER + ":" + userService.getUsernameById(id);
            redisService.del(key);
            redisService.del(key2);
    }

    @Override
    public UserDO getUser(String id) {
        String key = REDIS_KEY_USER + ":" + id;
        return (UserDO) redisService.get(key);
    }

    @Override
    public void setUser(UserDO user) {
        String key = REDIS_KEY_USER + ":" + user.getId();
        String key2 = REDIS_KEY_USER + ":" + user.getUsername();
        redisService.set(key, user, REDIS_EXPIRE);
        redisService.set(key2,user.getId(),REDIS_EXPIRE);
    }

    @Override
    public String getId(String username) {
        String key2 = REDIS_KEY_USER + ":" + username;
        return (String) redisService.get(key2);
    }
    @Override
    public void setId(String username, String id){
        String key2 = REDIS_KEY_USER + ":" + username;
        redisService.set(key2,id,REDIS_EXPIRE);
    }

    @Override
    public void setVoidUser(String id) {
        String key = REDIS_KEY_USER + ":" + id;
        redisService.set(key,new UserDO(),REDIS_EXPIRE);
    }
}
